package dk.hojlind.hackhunt.view;

import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import dk.hojlind.hackhunt.control.RegistryHandler;

public class DeleteDialog extends JDialog {

	//Public variables
	
		//Private variables
		RegistryHandler handler;
		String result;

		public DeleteDialog(RegistryHandler handler)
		{
			this.handler = handler;
			initUI();
		}

		private void initUI()
		{
			final JFrame frame = new JFrame();
			//Called to position components absolutely
			setLayout(null);
			
			//Set size and behavior of dialog
			setTitle("Register for Hack Hunt");
			setDefaultCloseOperation(DISPOSE_ON_CLOSE);
			setModalityType(ModalityType.APPLICATION_MODAL);
			setLocationRelativeTo(null);
			setSize(300, 250);

			//Create labels and textfields
			JLabel lblUser = new JLabel("Username");
			lblUser.setBounds(20, 20, 240, 30);
			
			final JTextField txtUser = new JTextField();
			txtUser.setBounds(20, 50, 240, 30);
			
			JLabel lblPass = new JLabel("Password");
			lblPass.setBounds(20, 100, 240, 30);
			
			final JPasswordField txtPass = new JPasswordField();
			txtPass.setBounds(20, 130, 240, 30);
			
			JButton accept = new JButton("Accept");
			accept.setBounds(160, 180, 100, 30);
			
			accept.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent event){
					
					//MUST CHANGE THE WAY THE PASSWORD IS HANDLED!!!
					result = handler.deleteUser(txtUser.getText(), txtPass.getText());
					
					if(!result.contains("error"))
					{
						dispose();
					}
					else
					{
						JOptionPane.showMessageDialog(frame, result);
					}
				}
			});

			JButton cancel = new JButton("Cancel");
			cancel.setBounds(20, 180, 100, 30);
			
			cancel.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent event){
					dispose();
				}
			});

			
			//Add everything
			add(lblUser);
			add(txtUser);
			add(lblPass);
			add(txtPass);
			add(accept);
			add(cancel);
			
		}
	
}
